﻿using Common.Middleware;
using KnifeUI.Swagger.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;

namespace Common;

public static class CommonWebApplicationExtensions
{
    public static void ConfigurationApplication(this WebApplication app)
    {
        if (app.Environment.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
   
        }

        app.UseSwagger(c =>
        {
            // c.SerializeAsV2 = true;
            
        });
        app.UseSwaggerUI();

        app.UseKnife4UI(c =>
        {
            // c.ConfigObject.
            c.RoutePrefix = ""; // serve the UI at root
            c.SwaggerEndpoint("/v1/api-docs", "V1 Docs");
        });

        app.UseMiddleware<UnAuthorizeExceptionMiddleware>();

        app.UseRouting();
        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(c =>
        {
            c.MapControllers();
            c.MapSwagger("{documentName}/api-docs");
        });
    }
}